library ItemEvent initializer onInit
// ================================================================

    public function interface Response takes unit u, item it returns nothing

// ================================================================

    globals
        private Response array pickupItemResp
        private integer pickupItemRespCount = 0

        private Response array dropItemResp
        private integer dropItemRespCount = 0

        private Response array useItemResp
        private integer useItemRespCount = 0

        private Response array pawnItemResp
        private integer pawnItemRespCount = 0
    endglobals

    function RegisterPickupItemResponse takes Response r returns nothing
        set pickupItemResp[pickupItemRespCount] = r
        set pickupItemRespCount = pickupItemRespCount + 1
    endfunction

    function RegisterDropItemResponse takes Response r returns nothing
        set dropItemResp[dropItemRespCount] = r
        set dropItemRespCount = dropItemRespCount + 1
    endfunction

    function RegisterUseItemResponse takes Response r returns nothing
        set useItemResp[useItemRespCount] = r
        set useItemRespCount = useItemRespCount + 1
    endfunction

    function RegisterPawnItemResponse takes Response r returns nothing
        set pawnItemResp[pawnItemRespCount] = r
        set pawnItemRespCount = pawnItemRespCount + 1
    endfunction

    // ================================================================

    private function pickupItem takes nothing returns nothing
        local integer i = 0
        loop
            exitwhen i >= pickupItemRespCount
            call pickupItemResp[i].evaluate(GetTriggerUnit(), GetManipulatedItem())
            set i=i+1
        endloop
    endfunction

    private function dropItem takes nothing returns nothing
        local integer i = 0

        if (GetWidgetLife(GetManipulatedItem()) <= 0) then
            call RemoveItem(GetManipulatedItem())
            return
        endif

        loop
            exitwhen i >= dropItemRespCount
            call dropItemResp[i].evaluate(GetTriggerUnit(), GetManipulatedItem())
            set i=i+1
        endloop
    endfunction

    private function useItem takes nothing returns nothing
        local integer i = 0

        loop
            exitwhen i >= useItemRespCount
            call useItemResp[i].evaluate(GetTriggerUnit(), GetManipulatedItem())
            set i=i+1
        endloop
    endfunction

    private function pawnItem takes nothing returns nothing
        local integer i = 0

        loop
            exitwhen i >= pawnItemRespCount
            call pawnItemResp[i].evaluate(GetTriggerUnit(), GetManipulatedItem())
            set i=i+1
        endloop
    endfunction

// ================================================================

    private function InitTrigger takes playerunitevent e, code c returns nothing
        local trigger t=CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ( t, e )
        call TriggerAddCondition(t, Condition(c))
        set t=null
    endfunction

    private function onInit takes nothing returns nothing
        call InitTrigger(EVENT_PLAYER_UNIT_PICKUP_ITEM, function pickupItem)
        call InitTrigger(EVENT_PLAYER_UNIT_DROP_ITEM, function dropItem)
        call InitTrigger(EVENT_PLAYER_UNIT_USE_ITEM, function useItem)
        call InitTrigger(EVENT_PLAYER_UNIT_PAWN_ITEM, function pawnItem)
    endfunction

endlibrary
